package com.wanmait.cookbook.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wanmait.cookbook.dao.CookbookCollectDAO;
import com.wanmait.cookbook.dao.CookbookDAO;
import com.wanmait.cookbook.dao.IngredientsConsumptionDAO;
import com.wanmait.cookbook.dao.IngredientsTypeDAO;
import com.wanmait.cookbook.dao.IngredientsTypeSecondDAO;
import com.wanmait.cookbook.dao.RecipeTypeDAO;
import com.wanmait.cookbook.dao.RecipeTypeSecondDAO;
import com.wanmait.cookbook.dao.UserinfoDAO;
import com.wanmait.cookbook.dao.WorksCollectDAO;
import com.wanmait.cookbook.dao.WorksDAO;
import com.wanmait.cookbook.vo.Cookbook;
import com.wanmait.cookbook.vo.IngredientsType;
import com.wanmait.cookbook.vo.IngredientsTypeSecond;
import com.wanmait.cookbook.vo.Pager;
import com.wanmait.cookbook.vo.RecipeType;
import com.wanmait.cookbook.vo.RecipeTypeSecond;
import com.wanmait.cookbook.vo.Userinfo;
import com.wanmait.cookbook.vo.Works;

/**
 * Servlet implementation class HomeServlet
 */
@WebServlet("/indexServlet")
public class IndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public IndexServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String action=request.getParameter("action");
		if("init".equals(action)){
			this.init(request, response);//ajax网页头菜谱分类 
		}else if ("initIngredients".equals(action)) {// ajax锟斤拷页头锟斤拷食锟侥的凤拷锟斤拷
			this.initIngredients(request, response);
		} else if ("getIndexHotCookbook".equals(action)) {// 锟斤拷锟斤拷页
			this.getIndexHotCookbook(request, response);
		} else if ("getAllRecipeType".equals(action)) {// 锟斤拷锟阶碉拷锟斤拷页锟斤拷展示
			this.getAllRecipeType(request, response);
		} else if ("getAllIngredientType".equals(action)) {// 食锟侥碉拷锟斤拷页锟斤拷展示
			this.getAllIngredientType(request, response);
		}else if("getAllWorks".equals(action)) {//所有作品单独页面展示
			this.getAllWorks(request, response);
		}else if("detailFood".equals(action)) {//鐐瑰嚮椋熸潗鏌ユ壘椋熻氨
			this.detailFood(request, response);
		} else if("detailCookbook".equals(action)) {
			this.detailCookbook(request, response);
		}else if("detailUserinfo".equals(action)) {
			this.detailUserinfo(request, response);
		}else if("detailWorks".equals(action)) {
			this.detailWorks(request, response);
		}else if("recipetypeSecond".equals(action)) {
			this.recipetypeSecondCookbook(request, response);
		}else if("isCollect".equals(action)) {
			this.isCollect(request, response);
		}else if("recipetypeSecondCookbookElse".equals(action)){
			this.recipetypeSecondCookbookElse(request, response);
		}else {
			this.getIndexHotCookbook(request, response);// 锟斤拷锟斤拷页
		}
	}
	//确定菜谱是否被收藏
	public void isCollect(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String userinfoId=request.getParameter("userinfoId");
		String cookbookId=request.getParameter("cookbookId");
		boolean idCollect =CookbookCollectDAO.getCollectCookbookCollectDAO().isCollect(Integer.parseInt(userinfoId),Integer.parseInt(cookbookId));
		
		
	}
	
	//鐐瑰嚮浣滃搧鍚嶇О鎴栧浘鐗囨煡鎵句綔鍝�
			public void detailWorks(HttpServletRequest request, HttpServletResponse response)
					throws ServletException, IOException {
				String name=request.getParameter("name");
				List<Works> detailWorkss = WorksDAO.getWorksDAO().detailWorks(name);
				List<Integer> worksCollectCounts=new ArrayList<Integer>();
				for (Works works : detailWorkss) {
					int count=WorksCollectDAO.getWorksCollectDAO().worksCollectCount(works);
					worksCollectCounts.add(count);
				}
				request.setAttribute("detailWorkss",detailWorkss);
				request.setAttribute("worksCollectCounts", worksCollectCounts);
				request.setAttribute("name", name);
				request.getRequestDispatcher("/detailWorks.jsp").forward(request, response);
			
			
			}
	//鐐瑰嚮鐢ㄦ埛鏌ョ湅鐢ㄦ埛璇︾粏淇℃伅
	public void detailUserinfo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String name=request.getParameter("name");
		List<Userinfo> detailUserinfos= UserinfoDAO.getUserinfoDAO().detailUserinfo(name);
		request.setAttribute("detailUserinfos",detailUserinfos);
		request.setAttribute("name", name);
		request.getRequestDispatcher("/detailUserinfo.jsp").forward(request, response);
	
	
	}
	//ajax跳转其他页
	public void recipetypeSecondCookbookElse(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String name=request.getParameter("name");
		String pageNo=request.getParameter("pageNo");
		Pager pager=new Pager();
		pager.setPageSize(2);
		int rows=CookbookDAO.getCookbookDAO().TypeCookbookRows(name);
		
		pager.setPageCount(rows);
	
		if(Integer.parseInt(pageNo)<1) {
			pageNo=1+"";
		}
		if(Integer.parseInt(pageNo)>pager.getPageCount()) {
			pageNo=pager.getPageCount()+"";
		}
		pager.setPageNo(Integer.parseInt(pageNo));
	
		List<Cookbook> picToCookbooks=CookbookDAO.getCookbookDAO().TypeCookbook(name,pager);
		List<Integer> foodToCookCounts=new ArrayList<>();
		for (Cookbook picToCookbook : picToCookbooks) {
			int count=CookbookCollectDAO.getCollectCookbookCollectDAO().coolectCount(picToCookbook);
			foodToCookCounts.add(count);
		}
		request.setAttribute("pager", pager);
		request.setAttribute("picToCookbooks",picToCookbooks);
		request.setAttribute("foodToCookCounts", foodToCookCounts);
		request.setAttribute("name", name);
		request.getRequestDispatcher("inc/ajaxInc/recipeCookbookInc.jsp").forward(request, response);
	
	
	}
	//recipetypeSecond
	//鐐瑰嚮鑿滅被鍨嬭烦杞彍璋�
	//跳转第一页
	public void recipetypeSecondCookbook(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String name=request.getParameter("name");
		Pager pager=new Pager();
		pager.setPageNo(1);
		pager.setPageSize(2);
		int rows=CookbookDAO.getCookbookDAO().TypeCookbookRows(name);
		pager.setPageCount(rows);
		List<Cookbook> picToCookbooks=CookbookDAO.getCookbookDAO().TypeCookbook(name,pager);
		
		List<Integer> foodToCookCounts=new ArrayList<>();
		for (Cookbook picToCookbook : picToCookbooks) {
			int count=CookbookCollectDAO.getCollectCookbookCollectDAO().coolectCount(picToCookbook);
			foodToCookCounts.add(count);
		}
		request.setAttribute("pager", pager);
		request.setAttribute("picToCookbooks",picToCookbooks);
		request.setAttribute("foodToCookCounts", foodToCookCounts);
		request.setAttribute("name", name);
		request.getRequestDispatcher("/detailCookbook.jsp").forward(request, response);
	
	
	}
	//鐐瑰嚮鑿滆氨鍚嶇О鎴栧浘鐗囨煡鎵鹃璋�
		public void detailCookbook(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			String name=request.getParameter("name");
			List<Cookbook> picToCookbooks=CookbookDAO.getCookbookDAO().picToCookbook(name);
			List<Integer> foodToCookCounts=new ArrayList<>();
			for (Cookbook picToCookbook : picToCookbooks) {
				int count=CookbookCollectDAO.getCollectCookbookCollectDAO().coolectCount(picToCookbook);
				foodToCookCounts.add(count);
			}
			request.setAttribute("picToCookbooks",picToCookbooks);
			request.setAttribute("foodToCookCounts", foodToCookCounts);
			request.setAttribute("name", name);
			request.getRequestDispatcher("/detailCookbook.jsp").forward(request, response);
		
		
		}
	//鐐瑰嚮椋熸潗鏌ユ壘椋熻氨
	public void detailFood(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String name=request.getParameter("name");
		List<Cookbook> foodToCookbooks=IngredientsConsumptionDAO.getIngredientsConsumptionDAO().foodToCookbookOnepager(name);
		List<Integer> foodToCookCounts=new ArrayList<>();
		for (Cookbook foodToCookbook : foodToCookbooks) {
			int count=CookbookCollectDAO.getCollectCookbookCollectDAO().coolectCount(foodToCookbook);
			foodToCookCounts.add(count);
		}
		
		
		request.setAttribute("foodToCookbooks",foodToCookbooks);
		request.setAttribute("foodToCookCounts", foodToCookCounts);
		request.setAttribute("name", name);
		request.getRequestDispatcher("/detailFood.jsp").forward(request, response);
	
	
	}

	public void getAllWorks(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		List<Works> allWorks = WorksDAO.getWorksDAO().findAllWorks();
		request.setAttribute("allWorks", allWorks);
		request.getRequestDispatcher("/worksShow.jsp").forward(request, response);
	}

	public void getAllIngredientType(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		List<IngredientsType> ingredientsTypes = IngredientsTypeDAO.getIngredientsTypeDAO().findByIngredientsType();
		request.setAttribute("ingredientsTypes", ingredientsTypes);
		List<IngredientsTypeSecond> intgredientsTypeSeconds = IngredientsTypeSecondDAO.getIngredientsTypeSecondDAO()
				.findAllIngredientsTypeSecond();
		request.setAttribute("ingredientsTypeSeconds", intgredientsTypeSeconds);

		request.getRequestDispatcher("/foodsList.jsp").forward(request, response);

	}

	public void getAllRecipeType(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 锟斤拷锟斤拷一锟斤拷锟斤拷锟阶凤拷锟洁，锟斤拷锟斤拷锟叫凤拷锟斤拷锟斤拷锟斤拷锟斤拷示
		List<RecipeType> recipeTypes = RecipeTypeDAO.getRecipeTypeDAO().findByRecipeType();
		request.setAttribute("recipeTypes", recipeTypes);

		// 锟斤拷锟截讹拷锟斤拷锟斤拷锟阶凤拷锟洁，锟斤拷锟斤拷锟叫凤拷锟斤拷锟斤拷锟斤拷锟斤拷示
		List<RecipeTypeSecond> recipeTypeSeconds = RecipeTypeSecondDAO.getRecipeTypeSecondDAO()
				.findByRecipeTypeSecond();
		request.setAttribute("recipeTypeSeconds", recipeTypeSeconds);
		request.getRequestDispatcher("/recipeTypeShowList.jsp").forward(request, response);
	}

	// 锟斤拷锟脚诧拷锟阶猴拷锟斤拷品展示
	public void getIndexHotCookbook(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// TODO Auto-generated method stub
		//杞挱鍥惧睍绀�
		HttpSession session=request.getSession();

		//轮播图

		List<Cookbook> carousels=CookbookDAO.getCookbookDAO().getCarousels();
		request.setAttribute("carousels", carousels);
		
		//食谱
		List<Cookbook> cookbooks = CookbookDAO.getCookbookDAO().getAllHotCookbook();
		List<Integer> cookbookCount = new ArrayList<Integer>();
		List<Boolean> isCollects=new ArrayList<Boolean>();
		Userinfo userinfo=(Userinfo) session.getAttribute("userinfo");
		for (Cookbook cookbook : cookbooks) {
			int count = CookbookCollectDAO.getCollectCookbookCollectDAO().coolectCount(cookbook);
			cookbookCount.add(count);
			if(userinfo!=null) {
			
				boolean isCollect=CookbookCollectDAO.getCollectCookbookCollectDAO().isCollect(userinfo.getId(), cookbook.getId());
				isCollects.add(isCollect);
			}
		}
		request.setAttribute("isCollects", isCollects);
		request.setAttribute("cookbookCount", cookbookCount);
		request.setAttribute("cookbooks", cookbooks);

		//作品
		List<Works> allWorks = WorksDAO.getWorksDAO().findAllWorks();
		List<Integer> worksCollectCounts=new ArrayList<Integer>();
		for (Works works : allWorks) {
			int count=WorksCollectDAO.getWorksCollectDAO().worksCollectCount(works);
			worksCollectCounts.add(count);
		}
		request.setAttribute("worksCollectCounts", worksCollectCounts);
		request.setAttribute("allWorks", allWorks);
		
		
		// 食材
		List<IngredientsTypeSecond> ingredientsTypeSeconds = IngredientsTypeSecondDAO.getIngredientsTypeSecondDAO()
				.findAllIngredientsTypeSecond();
		request.setAttribute("ingredientsTypeSeconds", ingredientsTypeSeconds);
		request.getRequestDispatcher("/index.jsp").forward(request, response);
	}

	// ajax锟斤拷锟斤拷食锟侥讹拷锟斤拷锟斤拷锟斤拷,转锟斤拷head.jsp页锟斤拷
	public void initIngredients(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		List<IngredientsType> ingredientsTypes = IngredientsTypeDAO.getIngredientsTypeDAO().findByIngredientsType();
		List<IngredientsTypeSecond> ingredientsTypeSeconds = IngredientsTypeSecondDAO.getIngredientsTypeSecondDAO()
				.findAllIngredientsTypeSecond();
		request.setAttribute("ingredientsTypeSeconds", ingredientsTypeSeconds);
		request.setAttribute("ingredientsTypes", ingredientsTypes);
		request.getRequestDispatcher("/inc/ajaxInc/ingredientTypeSecondInc.jsp").forward(request, response);

	}

	// ajax  食谱分类
	public void init(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		List<RecipeType> recipeTypes = RecipeTypeDAO.getRecipeTypeDAO().findByRecipeType();
		List<RecipeTypeSecond> recipeTypeSeconds = RecipeTypeSecondDAO.getRecipeTypeSecondDAO()
				.findByRecipeTypeSecond();
		request.setAttribute("recipeTypeSeconds", recipeTypeSeconds);
		request.setAttribute("recipeTypes", recipeTypes);
		request.getRequestDispatcher("/inc/ajaxInc/recipeTypeSecondInc.jsp").forward(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
